package cn.dw.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import cn.dw.domain.CityInfo;
import cn.dw.service.WeatherService;
import cn.dw.utils.HttpUtils;

public class weather {

	public static void main(String[] arg0) {
		weatherByCity("丽江天气");
	}
	
	private static WeatherService ws;
	
	public static String weatherByCity(String city){
		
		String Weather = null;
		
		String host = "http://saweather.market.alicloudapi.com";
	    String path = "/area-to-weather";
	    String method = "GET";
	    String appcode = "85038184f8b34a27bd30a2ed990a632f";
	    Map<String, String> headers = new HashMap<String, String>();
	    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
	    headers.put("Authorization", "APPCODE " + appcode);
	    Map<String, String> querys = new HashMap<String, String>();
		try {

			String area = city.replace("天气", "");

			if(area.length()==0){
				Weather = "请输入带正确地点天气查询";
				return Weather;
			}
			
			List<CityInfo> cityInfo = ws.getAll() ;
			
			String areaId = "";			
			
			for (int i = 0; i < cityInfo.size(); i++) {
				CityInfo cityInfo2 = cityInfo.get(i);
				if(area.contains(cityInfo2.getNameCN())){
					areaId = cityInfo2.getAreaId();
					area = cityInfo2.getNameCN();
				}
			}
			
			if(areaId.length()==0){
				Weather = "对不起，你输入的地点无法找到，请输入正确的地点名称";
				return Weather;
			}
			
			querys.put("areaid", areaId);
			
			querys.put("need3HourForcast", "0");
		    querys.put("needAlarm", "1");
		    querys.put("needHourData", "0");
		    querys.put("needIndex", "1");
		    querys.put("needMoreDay", "0");
			
			HttpResponse response = HttpUtils.doGet(host, path, method,
					headers, querys);

			String result = EntityUtils.toString(response.getEntity());

			JsonParser jp = new JsonParser();

			JsonElement je = jp.parse(result);

			JsonObject jo = je.getAsJsonObject();

			je = jo.get("showapi_res_body");

			jo = je.getAsJsonObject();
			
			JsonElement todayweather = jo.get("f1");
			
			JsonObject todaywather_info = todayweather.getAsJsonObject();
			
			
			String night_air_temperature = todaywather_info.get("night_air_temperature").toString().replace("\"", "");
			
			String day_air_temperature = todaywather_info.get("day_air_temperature").toString().replace("\"", "");

			
			String day_weather = todaywather_info.get("day_weather").toString().replace("\"", "");
			
			String night_weather = todaywather_info.get("night_weather").toString().replace("\"", "");

			String dayString = todaywather_info.get("day").toString().replace("\"", "");
			
			JsonElement index = todaywather_info.get("index");
			
			JsonObject index_info = index.getAsJsonObject();
			
			je = index_info.get("clothes");
			
			jo = je.getAsJsonObject();
			
			String clothes_title = jo.get("title").toString().replace("\"", "");
			
			String clothes_desc = jo.get("desc").toString().replace("\"", "");
			
			je = index_info.get("cold");
			
			jo = je.getAsJsonObject();
			
			String cold_title = jo.get("title").toString().replace("\"", "");
			
			String cold_desc = jo.get("desc").toString().replace("\"", "");
			
			Integer data = new Integer(dayString);
			
			dayString = "日期："+(data/10000)+"年"+((data%10000)/100)+"月"+(data%100)+"日";
			
			Weather = dayString + "\n"
					+ "白天天气：" + day_weather + "\n"
					+ "晚上天气：" + night_weather+"\n"
					+ "今日温度：" + night_air_temperature + "°~" + day_air_temperature + "°\n"
					+ "穿衣指数：" + clothes_title + "," + clothes_desc + "\n"
					+ "感冒指数：" + cold_title + "," + cold_desc + "\n"

					;

			System.out.println(todaywather_info);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return Weather;
		
	
	}

	public WeatherService getWs() {
		return ws;
	}

	@SuppressWarnings("static-access")
	public void setWs(WeatherService ws) {
		this.ws = ws;
	}
	
	
}
